import { defineStore } from "pinia";
import { EnumViewType } from "@renderer/assets/types";
import { idHelper } from "@renderer/store/pinia/helper";

interface IViewType {
  viewId: string;
  viewType: EnumViewType;
  seriesId: string;
  patientUID: string;
  tabId: string;
  position: string;
  groupId: string;
  viewMode: string;
}

const useViewManagerStore = defineStore("viewManager", {
  state: () => {
    return {
      tabSubView: {} as Record<string, IViewType[]>, // 当前tab需要显示的view，由viewSetting决定
      tabActiveViewIdObj: {} as Record<string, IViewType>, // tabId 所对应的 view
      tabWaitingToggleSubView:{} as Record<string, IViewType[]>, // tab无需显示，等切换出来的view，如 quick视图的 stack/ax/sag/cor 等。
    };
  },

  actions: {
    toggleWaitingView({
      tabId,
      viewId,
                      }){
      const waitingArr = this.tabWaitingToggleSubView[tabId];
      const target = waitingArr.filter((item) => item.viewId === viewId)[0];
      this.tabSubView[tabId] = [target]
    },

    // quick 视图中切换各个视图用。
    createWaitingToggleSubView({
                                      patientUID,
                                      seriesId,
                                      viewMode,
                                      viewTypeArr,
                                      tabId,
                                 groupId
                                    }) {

      const viewIdArr = viewTypeArr.map((viewType: string) => {
        const viewId = idHelper.createViewId({
          seriesId,
          viewMode,
          viewType,
        });
        return {
          viewId: viewId,
          viewType: viewType,
          viewMode,
          seriesId,
          patientUID,
          tabId,
          position: "",
          groupId,
        };
      });
      this.tabWaitingToggleSubView[tabId] = viewIdArr;
      this.tabSubView[tabId] = [viewIdArr[0]];
      this.setTabActiveViewId(tabId, structuredClone(viewIdArr[0]));
      return viewIdArr.map((view: any) => view.viewId)
    },

    batchCreateNormalMprView({
                            patientUID,
                            seriesId,
                            viewMode,
                            tabId,
                            groupId,
                          }){
      let viewIdArr: any[] = [];
      [EnumViewType.ax, EnumViewType.sag, EnumViewType.cor].map(viewType => {
        const viewId = idHelper.createViewId({
          seriesId,
          viewMode,
          viewType,
        },0);
        const viewInfo = {
          viewId,
          viewType,
          viewMode,
          seriesId,
          patientUID,
          tabId,
          position: '',
          groupId
        }
        viewIdArr.push(viewInfo);
      });
      this.tabSubView[tabId] = viewIdArr;
      this.setTabActiveViewId(tabId, structuredClone(viewIdArr[0]));
      return viewIdArr.map((view: any) => view.viewId)
    },

    // 更换Quick视图的layout后，批量生成新的layout对应的viewId
    batchCreateQuickView({
      patientUID,
      seriesId,
      viewMode,
      tabId,
      viewType,
      groupId,
      total = 1
    }) {
      let viewIdArr: any[] = [];
      const preTabSubView = this.tabSubView[tabId];
      for (let i = 0; i < total; i++) {
        const viewId = idHelper.createViewId({
          seriesId,
          viewMode,
          viewType,
        },i);
        let viewInfo :any = null;
        if (preTabSubView[i]){
          viewInfo = preTabSubView[i];
        }else {
          viewInfo = {
            viewId,
            viewType,
            viewMode,
            seriesId,
            patientUID,
            tabId,
            position: '',
            groupId
          }
        }
        viewIdArr.push(viewInfo);
      }
    this.tabSubView[tabId] = viewIdArr;
    },
    setTabActiveViewId(tabId: string, view: IViewType) {
      this.tabActiveViewIdObj[tabId] = view;
    },
  },
});

export { useViewManagerStore };
